﻿using Dos.ORM;
using Finance.Entity;
using Finance.IDal;
using Finance.Poco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Finance.Dal
{
    public class SaleDataDal : BaseDal, ISaleDataDal
    {
        static SaleDataDal()
        {
            AutoMapper.Mapper.CreateMap<ViewSaleData, ViewSaleDataEntity>();
            AutoMapper.Mapper.CreateMap<ViewSaleDataEntity, ViewSaleData>();
        }

        public bool Save(int userId, decimal amount, DateTime? saleDate, string remarks, string createUserName, int id)
        {
            SaleData saleData = new SaleData();
            saleData.Id = id;
            saleData.UserId = userId;
            saleData.Amount = amount;
            saleData.SaleDate = saleDate;
            saleData.Remarks = remarks;
            saleData.CreateTime = DateTime.Now;
            saleData.CreateUserName = createUserName;

            if (id == 0)
            {
                saleData.Attach(EntityState.Added);
            }
            else
            {
                saleData.Attach(EntityState.Modified);
            }
            return dbSession.Save<SaleData>(saleData) > 0;
        }

        public IEnumerable<Entity.ViewSaleDataEntity> List(string userName, DateTime? beginDate, DateTime? endDate, int? orgId, int pageIndex, int pageSize, out int total)
        {
            var sql = dbSession.From<ViewSaleData>();
            if (!string.IsNullOrWhiteSpace(userName))
            {
                sql.Where(v => v.UserName.Like(userName));
            }
            if (beginDate.HasValue)
            {
                sql.Where(v => v.SaleDate >= beginDate);
            }

            if (endDate.HasValue)
            {
                sql.Where(v => v.SaleDate < endDate);
            }

            if (orgId.HasValue)
            {

            }

            total = sql.Count();

            var data = sql.OrderByDescending(v => v.SaleDate).Page(pageSize, pageIndex).ToList();

            return data.Select(AutoMapper.Mapper.Map<ViewSaleDataEntity>).ToList();

        }

        public IEnumerable<Entity.ViewSaleDataSummaryEntity> ListSummary(int type, string userName, DateTime? beginDate, DateTime? endDate, int? orgId, int pageIndex, int pageSize, out int total)
        {
            string fmt = "";
            if (type == 1)
            {
                fmt = "%Y.%u";
            }
            else if (type == 2)
            {
                fmt = "%Y.%m";
            }
            else if (type == 3)
            {
                fmt = "%Y";
            }
            string text = @"select sd.userid,sum(sd.amount)amount,DATE_FORMAT(saledate,'" + fmt + @"') date,u.userName,u.email from sale_data sd join user u on sd.userid=u.id
group by sd.userid,date,u.userName,u.email
order by date desc";
            total = dbSession.FromSql("select count(*)from(" + text + ")t").ToScalar<int>();
            if (total == 0)
            {
                return new List<ViewSaleDataSummaryEntity>();
            }
            //var sql = dbSession.From<ViewSaleData>();
            //if (!string.IsNullOrWhiteSpace(userName))
            //{
            //    sql.Where(v => v.UserName.Like(userName));
            //}
            //if (beginDate.HasValue)
            //{
            //    sql.Where(v => v.SaleDate >= beginDate);
            //}

            //if (endDate.HasValue)
            //{
            //    sql.Where(v => v.SaleDate < endDate);
            //}

            //if (orgId.HasValue)
            //{

            //}

            //total = sql.Count();

            //var data = sql.OrderByDescending(v => v.SaleDate).Page(pageSize, pageIndex).ToList();

            return dbSession.FromSql(text + string.Format(" limit {0},{1}", (pageIndex - 1) * pageSize, pageSize)).ToList<ViewSaleDataSummaryEntity>();

             

        }

        public bool Delete(int id)
        {
            return dbSession.Delete<SaleData>(id) > 0;
        }
    }
}
